﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class RotationRuleDetailBLL : IBaseInfoDetail
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IRotationRuleDetailDA rotationRuleDetailDA = DataAccess.CreateRotationRuleDetailDA();

        public RotationRuleDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public RotationRuleDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public RotationRuleDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<RotationRuleDetailInfo> rotationRuleDetailInfos = new List<RotationRuleDetailInfo>();
                RotationRuleDetailInfo rotationRuleDetailInfo = null;
                for (i = 0; i < mainID.Count; i++)
                {
                    rotationRuleDetailInfo = rotationRuleDetailDA.GetRotationRuleDetailByID(dataBase, tran, mainID[i], subId[i], this.dataBaseID[0]);
                    rotationRuleDetailInfos.Add(rotationRuleDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (rotationRuleDetailDA.DeleteRotationRuleDetail(dataBase, tran, mainID, subId, this.dataBaseID) > 0)
                {
                    for (i = 0; mainID.Count > i; i++)
                    {
                        auditDataBLL.InsertAuditData<RotationRuleDetailInfo>(dataBase, tran, rotationRuleDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ROTATION_RULE_DETAIL", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + mainID[i] + "," + subId[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return rotationRuleDetailDA.GetAllRotationRuleDetail(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            return new DataSet();
        }

        public object GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            return rotationRuleDetailDA.GetRotationRuleDetailByID(mainID, subID, this.dataBaseID[0]);
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            List<RotationRuleDetailInfo> rotationRuleDatail = new List<RotationRuleDetailInfo>();
            List<object> objs = null;
            if ((rotationRuleDatail != null) && (rotationRuleDatail.Count > 0))
            {
                objs = new List<object>();
                for (int i = 0; i < rotationRuleDatail.Count; i++)
                {
                    objs.Add(rotationRuleDatail[i]);
                }
            }
            return objs;
        }

        public string InsertDetail(object obj)
        {
            RotationRuleDetailInfo rotationRuleDatail = obj as RotationRuleDetailInfo;
            string LineID = string.Empty;
            try
            {
                if (rotationRuleDetailDA.CheckRotationRuleDetailIDUnique(rotationRuleDatail.RotationRuleID, rotationRuleDatail.LotAttributes.ToString(), this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2420_002", this.pubParasInfo.UserLanguage, new List<string> { rotationRuleDatail.RotationRuleID, rotationRuleDatail.LineID.ToString() });
                }
                if (rotationRuleDatail == null)
                {
                    return LineID;
                }
                rotationRuleDatail.UpdatedBy = rotationRuleDatail.CreatedBy;
                rotationRuleDatail.CreatedDate = DateTime.Now;
                rotationRuleDatail.UpdatedDate = DateTime.Now;
                if (rotationRuleDetailDA.InsertRotationRuleDetail(obj as RotationRuleDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<RotationRuleDetailInfo>(null, rotationRuleDatail, AuditAction.Insert, this.pubParasInfo.UserID, "ROTATION_RULE_DETAIL", this.dataBaseID[0]);
                    LineID = rotationRuleDatail.LineID.ToString();
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }

        public string UpdateDetail(object obj)
        {
            string LineID = string.Empty;
            try
            {
                RotationRuleDetailInfo rotationRuleDatail = obj as RotationRuleDetailInfo;
                RotationRuleDetailInfo oldRotationRuleDetailInfo = rotationRuleDetailDA.GetRotationRuleDetailByID(rotationRuleDatail.RotationRuleID, rotationRuleDatail.LineID.ToString(), this.dataBaseID[0]);
                if (rotationRuleDetailDA.UpdateRotationRuleDetail(rotationRuleDatail, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<RotationRuleDetailInfo>(oldRotationRuleDetailInfo, rotationRuleDatail, AuditAction.Update, this.pubParasInfo.UserID, "ROTATION_RULE_DETAIL", this.dataBaseID[0]);
                    LineID = rotationRuleDatail.RotationRuleID + " , " + rotationRuleDatail.LineID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

